﻿using System;
using System.IO;
using NUnit.Framework;
using QuickTest.Core.TestRunner;

namespace QuickTest.Tests.Core.TestRunner
{
    [TestFixture]
    public class AssemblyResolverTests
    {
        private string appBasePath;

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            appBasePath = Environment.ExpandEnvironmentVariables(@"%temp%\AssemblyResolver");
            if (Directory.Exists(appBasePath))
            {
                Directory.Delete(appBasePath, true);
            }
            Directory.CreateDirectory(appBasePath);
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            Directory.Delete(appBasePath, true);
        }

        [Test]
        public void Can_AddFile()
        {
            AppDomain domain = null;
            try
            {
                domain = AppDomain.CreateDomain("QTAssemblyResolver", null, appBasePath, null, false);
                var assemblyResolver = (AssemblyResolver) domain.CreateInstanceFromAndUnwrap(typeof (AssemblyResolver).Assembly.CodeBase, typeof (AssemblyResolver).FullName);
                using (assemblyResolver)
                {
                    Type type = typeof (TestAttribute);
                    // ensure that assembly containing type can be found
                    assemblyResolver.AddFile(type.Assembly.CodeBase);
                    domain.CreateInstance(type.Assembly.FullName, type.FullName);
                }
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
    }
}